#pragma once
#include "requestor.h"
namespace rpc
{
    namespace client
    {
        class RpcCaller
        {
        public:
            using ptr = std::shared_ptr<RpcCaller>;
            using JsonAsyncResponse = std::future<Json::Value>;
            using JsonResponseCallback = std::function<void(const Json::Value &)>;
            RpcCaller(const Requestor::ptr &requestor) : _requestor(requestor)
            {
            }
            bool call(const BaseConnection::ptr &conn, const std::string &method, const Json::Value &param, Json::Value &result)
            {
                DLOG("开始同步rpc调用...");
                // 1.组织请求
                auto req_msg = MessageFactory::create<RpcRequest>();
                req_msg->setrId(UUID::uuid());
                req_msg->setMtype(MType::REQ_RPC);
                req_msg->Setmethod(method);
                req_msg->Setparams(param);
                BaseMessage::ptr resp_msg;
                // 2 发送请求
                bool ret = _requestor->send(conn, std::dynamic_pointer_cast<BaseMessage>(req_msg), resp_msg);
                if (ret == false)
                {
                    ELOG("同步Rpc请求失败");
                    return false;
                }

                DLOG("收到响应，进行解析，获取结果!");
                auto rpc_rsp_msg = std::dynamic_pointer_cast<RpcResponse>(resp_msg);
                if (!rpc_rsp_msg)
                {
                    ELOG("类型转换失败");
                    return false;
                }
                if (rpc_rsp_msg->rcode() != RCode::RCODE_OK)
                {
                    std::cout << (int)rpc_rsp_msg->rcode() << std::endl;
                    ELOG("RPC请求出错:%s", errReason(rpc_rsp_msg->rcode()));
                    return false;
                }
                result = rpc_rsp_msg->result();
                ELOG("结构设置完毕");
                return true;
            }
            bool call(const BaseConnection::ptr &conn, const std::string &method, const Json::Value &param, std::future<Json::Value> &result)
            {
                auto req_msg = MessageFactory::create<RpcRequest>();
                req_msg->setrId(UUID::uuid());
                req_msg->setMtype(MType::REQ_RPC);
                req_msg->Setmethod(method);
                req_msg->Setparams(param);
                BaseMessage::ptr resp_msg;
                // 2 发送请求
                auto json_promise = std::make_shared<std::promise<Json::Value>>();
                result = json_promise->get_future();
                Requestor::RequestCallBack cb = std::bind(&RpcCaller::Callback, this, json_promise, std::placeholders::_1);
                bool ret = _requestor->send(conn, std::dynamic_pointer_cast<BaseMessage>(req_msg), cb);
                if (ret == false)
                {
                    ELOG("同步Rpc请求失败");
                    return false;
                }
                return true;
            }
            bool call(const BaseConnection::ptr &conn, const std::string &method, const Json::Value &param, const JsonResponseCallback &cb)
            {
                auto req_msg = MessageFactory::create<RpcRequest>();
                req_msg->setrId(UUID::uuid());
                req_msg->setMtype(MType::REQ_RPC);
                req_msg->Setmethod(method);
                req_msg->Setparams(param);
                BaseMessage::ptr resp_msg;

                Requestor::RequestCallBack req_cb = std::bind(&RpcCaller::Callback1, this, cb, std::placeholders::_1);
                bool ret = _requestor->send(conn, std::dynamic_pointer_cast<BaseMessage>(req_msg), req_cb);
                if (ret == false)
                {
                    ELOG("回调请求失败!");
                    return false;
                }
                return true;
            }

        private:
            void Callback1(const JsonResponseCallback &cb, const BaseMessage::ptr &msg)
            {
                auto rpc_rsp_msg = std::dynamic_pointer_cast<RpcResponse>(msg);
                if (!rpc_rsp_msg)
                {
                    ELOG("类型转换失败");
                    return;
                }
                if (rpc_rsp_msg->rcode() != RCode::RCODE_OK)
                {
                    ELOG("Rpc异步请求失败%s", errReason(rpc_rsp_msg->rcode()));
                }
            }
            void Callback(std::shared_ptr<std::promise<Json::Value>> result, const BaseMessage::ptr &msg)
            {
                auto rpc_rsp_msg = std::dynamic_pointer_cast<RpcResponse>(msg);
                if (!rpc_rsp_msg)
                {
                    ELOG("Rpc响应失败");
                    return;
                }
                if (rpc_rsp_msg->rcode() != RCode::RCODE_OK)
                {
                    ELOG("RPC异步请求失败%s", errReason(rpc_rsp_msg->rcode()));
                    return;
                }
                result->set_value(rpc_rsp_msg->result());
            }

        private:
            Requestor::ptr _requestor;
        };
    }
}